package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.model.*;
import com.chaodev.base.dao.DepartDao;
import com.chaodev.base.dao.RoleDao;
import com.chaodev.base.dao.UserDao;
import com.chaodev.base.dto.UserQueryDTO;
import com.chaodev.base.service.IUserService;
import com.chaodev.base.utils.MD5Util;
import org.apache.cxf.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private DepartDao departDao;
    @Autowired
    private RoleDao roleDao;


    @Override
    public UserInfo getUserMessNew(Map<String, Object> map) {
        return userDao.getUserMessNew(map);
    }

    @Override
    public PageInfo<UserQueryDTO> userQuery(DataPageVO dataPage) {
        PageHelper.startPage(dataPage.getPage(), dataPage.getRows());
        List<UserQueryDTO> list = userDao.userQuery(dataPage);
        PageInfo<UserQueryDTO> pageInfo = new PageInfo<>(list, dataPage.getRows());
        return pageInfo;
    }

    @Override
    public PageInfo<UserQueryDTO> userQueryByDId(DataPageVO dataPage){
        PageHelper.startPage(dataPage.getPage(), dataPage.getRows());
        List<UserQueryDTO> list = userDao.userQueryByDepatId(dataPage);
        PageInfo<UserQueryDTO> pageInfo = new PageInfo<>(list, dataPage.getRows());
        return pageInfo;
    }

    @Override
    public PageInfo<UserRoleDepart> userRoleTableList(DataPageVO dataPage) {
        PageHelper.startPage(dataPage.getPage(), dataPage.getRows());
        List<UserRoleDepart> list = userDao.userRoleTableList(dataPage);
        PageInfo<UserRoleDepart> pageInfo = new PageInfo<>(list, dataPage.getRows());
        return pageInfo;
    }

    @Override
    public boolean userRoleAdd(UserRoleDepart userRoleDepart) {
        int result=userDao.userRoleAdd(userRoleDepart);
        return result>0;
    }

    @Override
    @Transactional
    public boolean changeRoleType(UserRoleDepart userRoleDepart) {
        int result2=userDao.changeDefaultRoleType(userRoleDepart);
        int result=userDao.changeRoleType(userRoleDepart);
        return result>0&&result2>0;
    }

    @Override
    public boolean userRoleDelete(UserRoleDepart userRoleDepart) {
        int result=userDao.userRoleDelete(userRoleDepart);
        return result>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean userAdd(User user) {
        Map<String, Object> map=new HashMap<>();
        map.put("P_ID",user.getId());
        map.put("P_USER_ID",user.getUserId());
        map.put("P_USER_NAME",user.getUserName());
        String s = null;
        try {
            s = MD5Util.md5Encode("000000").toUpperCase(Locale.ROOT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("P_PASSWORD",s);
        map.put("P_DEPART_ID",user.getDepartId());
        map.put("P_ROLE_ID",user.getRoleId());
        map.put("P_REAL_ACC_NBR",user.getRealAccNbr());
        map.put("P_EMAIL",user.getEmail());
        map.put("U_USER_ID",user.getCreateUser());
        int userCount=userDao.lookUser(map);
        boolean result=false;
        if(userCount==0){
            //新增
            if(StringUtils.isEmpty(user.getId()))
            {
                int result1=userDao.userAdd(map);
                int result2=userDao.userDepartAdd(map);
                result=result1>0&&result2>0;
            }
            else{
                int result1=userDao.userUpdate(map);
                int result2=userDao.userDepartUpdate(map);
                result=result1>0&&result2>0;
            }
        }
        return result;
    }


    @Override
    public boolean modifySkin(Map<String, Object> map) {
        int result=userDao.modifySkin(map);
        return result>0;
    }

    @Override
    @Transactional
    public boolean userDelete(User user) {
        int result=userDao.userDelete(user);
        int result2=userDao.userDepartDelete(user);
        return result>0&&result2>0;
    }


    @Override
    public int ValidUserOldPwd(Map<String, Object> map) {
        return userDao.ValidUserOldPwd(map);
    }

    @Override
    public boolean userPwdUpdate(Map<String, Object> map) {
        int result=userDao.userPwdUpdate(map);
        return result>0;
    }

    @Override
    public boolean updatePassword(Map<String, Object> map) {
        int result=userDao.updatePassword(map);
        return result>0;
    }

    @Override
    public boolean userJieSuo(Map<String, Object> map) {
        int result=userDao.userJieSuo(map);
        return result>0;
    }

    @Override
    public int ValidUserDepartRole(Map<String, Object> map) {
        return userDao.ValidUserDepartRole(map);
    }

    @Override
    public List<UserInfo> UserRoleChange(Map<String, Object> map) {
        return userDao.UserRoleChange(map);
    }
    @Override
    public boolean userStop(User user) {
        int result=userDao.userStop(user);
        return result>0;
    }


    @Override
    public List<Map<String, Object>> getUsersByDepartTree(String departId) {
        List<Map<String, Object>> result = new ArrayList<>();
        Depart depart = departDao.getDepartAndUserById(departId);
        if (depart != null){
            Map<String, Object> departMap = new HashMap<>();
            departMap.put("value",depart.getDepartId());
            departMap.put("label",depart.getDepartName());
//            List<User> users = userDao.getUsersByDepartmentId(depart.getDepartId());
//            departMap.put("userList",users);

//            递归
            List<Map<String, Object>> children = new ArrayList<>();
            List<Depart> childDepartments = departDao.getChildrenByParentId(depart.getDepartId());
            for (Depart department : childDepartments) {
                children.addAll(getUsersByDepartTree(department.getDepartId()));
            }
            if(children.size()>0) {
                departMap.put("children", children);
            }
            result.add(departMap);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getUsersByRole() {
        List<Map<String, Object>> users = userDao.selectUsersByRole();
        return users;
    }
}
